package com.tenton.service.impl;

import com.tenton.mapper.CommodityMapper;
import com.tenton.mapper.MonthlySalesMapper;
import com.tenton.pojo.Commodity;
import com.tenton.pojo.MonthlySales;
import com.tenton.service.MonthlySalesService;
import com.tenton.utils.Constants;
import com.tenton.utils.DateUtils;
import com.tenton.vo.MonthlySalesReqVo;
import com.tenton.vo.SalePolyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Date: 2021/4/2 16:04
 * @Author: Tenton
 * @Description:
 */
@Service
public class MonthlySalesServiceImpl implements MonthlySalesService {
    @Autowired
    CommodityMapper commodityMapper;
    @Autowired
    MonthlySalesMapper monthlySalesMapper;

    @Override
    public Map<String, Object> polyDate() {
        //所有商品信息
        List<Commodity> commodityList = commodityMapper.selectAll();
        Map<String,Object> data = new HashMap<>();
        List<SalePolyVo> list = new ArrayList<>();
        List<String> nameList = new ArrayList<>();
        for (Commodity commodity : commodityList) {
            SalePolyVo vo = new SalePolyVo();
            vo.setName(commodity.getComName());
            vo.setType(Constants.POLY);
            List<Integer> numList = this.halfYearSales(commodity.getComName());
            vo.setData(numList);
            list.add(vo);
            nameList.add(commodity.getComName());
        }
        List<String> dateList = this.currentMonthDate();
        data.put(Constants.COMMODITY_KEY,list);
        data.put(Constants.NAME_KEY,nameList);
        data.put(Constants.TIME_KEY,dateList);
        return data;
    }

    /**
     * 获取近半年销量
     *
     * @param comName
     */
    public List<Integer> halfYearSales(String comName){
        //存储销量
        List<Integer> list = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        int one = this.currentMonthSale(c, comName, Constants.CURRENT_MONTH);
        int two = this.currentMonthSale(c, comName, Constants.LAST_MONTH);
        int three = this.currentMonthSale(c, comName, Constants.LAST_MONTH);
        int four = this.currentMonthSale(c, comName, Constants.LAST_MONTH);
        int five = this.currentMonthSale(c, comName, Constants.LAST_MONTH);
        int six = this.currentMonthSale(c, comName, Constants.LAST_MONTH);
        list.add(six);
        list.add(five);
        list.add(four);
        list.add(three);
        list.add(two);
        list.add(one);
        return list;
    }

    /**
     * 近半年月份
     * @return
     */
    public List<String> currentMonthDate(){
        List<String> list = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        //本月
        c.add(Calendar.MONTH, Constants.CURRENT_MONTH);
        String one = sdf.format(c.getTime());
        //上一月
        c.add(Calendar.MONTH, Constants.LAST_MONTH);
        String two = sdf.format(c.getTime());
        //上第二月
        c.add(Calendar.MONTH, Constants.LAST_MONTH);
        String three = sdf.format(c.getTime());
        //上第三月
        c.add(Calendar.MONTH, Constants.LAST_MONTH);
        String four = sdf.format(c.getTime());
        //上第四月
        c.add(Calendar.MONTH, Constants.LAST_MONTH);
        String five = sdf.format(c.getTime());
        //上第五月
        c.add(Calendar.MONTH, Constants.LAST_MONTH);
        String six = sdf.format(c.getTime());
        list.add(six);
        list.add(five);
        list.add(four);
        list.add(three);
        list.add(two);
        list.add(one);
        return list;
    }
    /**
     * 获取当前月销量
     * @param c
     * @param comName
     * @param month
     * @return
     */
    public int currentMonthSale(Calendar c,String comName,int month){
        MonthlySalesReqVo vo = new MonthlySalesReqVo();
        //vo赋值
        vo.setComName(comName);
        try {
            //每月最初一天
            c.add(Calendar.MONTH, month);
            c.set(Calendar.DAY_OF_MONTH, Constants.DAY_OF_MONTH_START);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startTime = sdf.format(c.getTime());
            vo.setStartTime(DateUtils.setStartDate(sdf.parse(startTime)));
            //每月最后一天
            c.add(Calendar.MONTH, Constants.MONTH);
            c.set(Calendar.DAY_OF_MONTH, Constants.DAY_OF_MONTH_END);
            String entTime = sdf.format(c.getTime());
            vo.setSaleDate(sdf.parse(entTime));
            vo.setEndTime(DateUtils.setEndDate(sdf.parse(entTime)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        MonthlySales monthlySales = monthlySalesMapper.selectByParam(vo);
        if (monthlySales == null){
            return Constants.MONTH_SALE_NUM;
        }else {
            return monthlySales.getNum();
        }
    }
}
